package com.ablog.service.impl;

import com.ablog.model.Comment;
import com.ablog.dao.CommentMapper;
import com.ablog.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.date.DateUtil;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Comment)表服务实现类
 *
 * @author mopeiwen
 * @since 2020-07-22 13:11:20
 */
@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Comment queryById(String id) {
        return this.commentMapper.selectByPrimaryKey(id);
    }
    
    /**
     * 通过Comment对象查询集合
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public List<Comment> queryAll(Comment comment) {
        return this.commentMapper.selectAll(comment);
    }
    
    @Override
    public PageInfo<Comment> queryAll(Comment comment,int pagesize,int pagenum) {
    	//pageNum代表页码值，pageSize代表每页条数
    	PageHelper.startPage(pagenum,pagesize);
    	//使用PageInfo来封装查询的数据
        PageInfo<Comment> pageInfo = new PageInfo<Comment>(this.commentMapper.selectAll(comment));
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public Comment add(Comment comment) {
    	comment.setAddtime(DateUtil.now());
    	
        this.commentMapper.insertSelective(comment);
        return comment;
    }

    /**
     * 修改数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public Comment update(Comment comment) {
        this.commentMapper.updateByPrimaryKeySelective(comment);
        return this.queryById(comment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean delById(String id) {
        return this.commentMapper.deleteByPrimaryKey(id) > 0;
    }
}